import classnames from 'classnames'
import Icon from '@/components/Icon'
import styles from './index.module.scss'
import Image from '@/components/Img'
import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime'
import 'dayjs/locale/zh-cn'
import { useDispatch, useSelector } from 'react-redux'
import { setMoreAction } from '@/store/actions/home'
import { useHistory } from 'react-router-dom'
dayjs.locale('zh-cn')
dayjs.extend(relativeTime)
const ArticleItem = ({ article }) => {
  const history = useHistory()
  const dispatch = useDispatch()
  const token = useSelector(state => !!state.login.token)
  const {
    cover: { type, images },
    title,
    aut_name,
    comm_count,
    pubdate
  } = article
  return (
    <div className={styles.root} onClick={() => history.push(`/article/${article.art_id}`)}>
      <div
        className={classnames(
          'article-content',
          type === 3 ? 't3' : '',
          type === 0 ? 'none-mt' : ''
        )}
      >
        <h3>{title}</h3>
        {type !== 0 && (
          <div className="article-imgs">
            {images.map((item, i) => (
              <div className="article-img-wrapper" key={i}>
                <Image src={item} alt="" />
              </div>
            ))}
          </div>
        )}
      </div>
      <div className={classnames('article-info', type === 0 ? 'none-mt' : '')}>
        <span>{aut_name}</span>
        <span>{comm_count} 评论</span>
        <span>{dayjs(pubdate).fromNow()}</span>

        <span className="close">
          {token && <Icon type="iconbtn_essay_close" onClick={() => dispatch(setMoreAction({
            visible: true,
            channelId: article.art_id
          }))} />}
        </span>
      </div>
    </div>
  )
}

export default ArticleItem
